package kr.ac.kaist.pointdaproject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.LocationManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.Toast;

public class Setup extends PreferenceActivity {
	private static final String TAG = "Setup";
	/*
	 * private Button btn;
	 */
	private Preference mGpsScreen;
	private Preference mWifiScreen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/*
		setContentView(R.layout.setup);
		btn = (Button)findViewById(R.id.setup_gps);
		btn.setText(checkGps());
		*/
		 addPreferencesFromResource(R.xml.setup_preference);
		 mGpsScreen = (Preference)getPreferenceScreen().findPreference("gps_setting_preferences");
		 mGpsScreen.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			 @Override
			 public boolean onPreferenceClick(Preference preference) {
				 changeGps();
				 return true;
			 }
		 });
		 mWifiScreen = (Preference)getPreferenceScreen().findPreference("wifi_setting_preferences");
		 mWifiScreen.setOnPreferenceClickListener(new OnPreferenceClickListener(){
			 @Override
			 public boolean onPreferenceClick(Preference preference) {
				 changeWifi();
				 return true;
			 }
		 });
	}

	@Override
	protected void onResume() {
		super.onResume();

		mGpsScreen.setEnabled(!isGPSEnabled());
		mWifiScreen.setEnabled(!isWifiEnabled());

        IntentFilter filter = new IntentFilter();
        filter.addAction(LocationManager.MODE_CHANGED_ACTION);
        filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        registerReceiver(mReceiver, filter);

		 /**
		 * btn.setText(checkGps());
		 */
	}

    @Override
    protected void onPause() {
        super.onPause();
        unregisterReceiver(mReceiver);
    }	

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
        	String action = intent.getAction();
        	 
         	if (LocationManager.MODE_CHANGED_ACTION.equals(action)) {
            	mGpsScreen.setEnabled(!isGPSEnabled());
         	} else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) {
         		mWifiScreen.setEnabled(!isWifiEnabled());
         	}
        }
    };

	public boolean isGPSEnabled(){
		boolean ret = false;
		
		LocationManager lm = (LocationManager) getSystemService(LOCATION_SERVICE);
		if(lm != null) {
			ret = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
		}
		return ret;
	}

	public void onClickSetup(View v) {
		changeGps();
	}


	private String checkGps() {
		String gpsSettings = android.provider.Settings.Secure.getString(
				getContentResolver(),
				android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		String str;

		if(gpsSettings.indexOf("gps", 0) < 0) 
		{
			str = "OFF";
		}
		else
		{
			str = "ON";
		}

		return str;

	}

	private void changeGps() {
		String gpsSettings = android.provider.Settings.Secure.getString(
				getContentResolver(),
				android.provider.Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

		Intent intent = new Intent(
				android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
		intent.addCategory(Intent.CATEGORY_DEFAULT);
		startActivity(intent);
	}

	public boolean isWifiEnabled(){
		boolean ret = false;
		
		WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
		if(wm != null) {
			ret = wm.isWifiEnabled();
		}
		return ret;
	}

	private void changeWifi() {
		WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
		if (wm != null) {
			wm.setWifiEnabled(!wm.isWifiEnabled());
		}
	}
}
